package servlet;

import bean.Order;
import service.impl.OrderServiceImpl;
import util.DataTypeConversion;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;

@WebServlet("/user/get_orderId_to_cancel_order")
public class GetOrderIdToCancelOrder extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderIdStr = request.getParameter("orderId");
        int orderId = Integer.parseInt(orderIdStr);
        OrderServiceImpl orderService = new OrderServiceImpl();
        Order order = orderService.getOrderById(orderId);
         Date startTime = order.getScreeningSchedule().getStartTime();
         Date endTime =  order.getScreeningSchedule().getEndTime();
         String startTimeStr = DataTypeConversion.conversionUtilDateToString(startTime);
         String endTimeStr = DataTypeConversion.conversionUtilDateToString(endTime);
         Timestamp  startTimestamp = Timestamp.valueOf(startTimeStr);
         Timestamp  endTimestamp = Timestamp.valueOf(endTimeStr);
        Timestamp now = new Timestamp(System.currentTimeMillis());
        if(now.after(endTimestamp)){
            String cancelOrderMessage =  "放映结束了你还想退票？滚";
            request.getSession().setAttribute("cancelOrderMessage",cancelOrderMessage);
            System.out.println(cancelOrderMessage);
       }else if(now.after(startTimestamp)){
            String cancelOrderMessage =  "放映开始了你还想退票？滚";
            request.getSession().setAttribute("cancelOrderMessage",cancelOrderMessage);
        }else {
            if(new OrderServiceImpl().deleteOrder(orderId)){
                String cancelOrderMessage = "退票成功";
                request.getSession().setAttribute("cancelOrderMessage",cancelOrderMessage);}
            else {
                String cancelOrderMessage =  "退票失败联系工作人员";
                request.getSession().setAttribute("cancelOrderMessage",cancelOrderMessage);
            }
        }
    }
}
